package com.jyszkj.map.filter

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.PI

object GreenFilterConverter {

    fun createComplexFilter(): ColorMatrixColorFilter {
        // 创建各个滤镜的矩阵（按CSS从右向左的顺序）
        val hueMatrix = createHueMatrix(300f)
        val invertMatrix = createInvertMatrix(0.89f) // 136% = 1.36
        val saturateMatrix = createSaturationMatrix(0.88f) // 110% = 1.10
        val sepiaMatrix = createSepiaMatrix(0.74f) // 227% = 2.27
        val contrastMatrix = createContrastMatrix(1.66f) // 205% = 2.05
        val brightnessMatrix = createBrightnessMatrix(0.85f) // 72% = 0.72
        val applyGrayscale = applyGrayscale(0.16f)

        // 合并矩阵（CSS滤镜是从右向左应用的）
        val finalMatrix = ColorMatrix().apply {
            postConcat(ColorMatrix(contrastMatrix))
            postConcat(ColorMatrix(brightnessMatrix))
            postConcat(ColorMatrix(sepiaMatrix))
            //postConcat(ColorMatrix(applyGrayscale))
            postConcat(ColorMatrix(invertMatrix))
            postConcat(ColorMatrix(hueMatrix))
        }

        return ColorMatrixColorFilter(finalMatrix)
    }

    fun applyGrayscale(amount: Float): FloatArray{
        val invAmount = 1f - amount
        val r = 0.213f * invAmount
        val g = 0.715f * invAmount
        val b = 0.072f * invAmount

        val grayscaleMatrix = floatArrayOf(
            r + amount, g, b, 0f, 0f,
            r, g + amount, b, 0f, 0f,
            r, g, b + amount, 0f, 0f,
            0f, 0f, 0f, 1f, 0f
        )
        return grayscaleMatrix
    }

    private fun createBrightnessMatrix(brightness: Float): FloatArray {
        return floatArrayOf(
            brightness, 0f,        0f,        0f, 0f,
            0f,        brightness, 0f,        0f, 0f,
            0f,        0f,        brightness, 0f, 0f,
            0f,        0f,        0f,        1f, 0f
        )
    }

    private fun createContrastMatrix(contrast: Float): FloatArray {
        val adjust = ((-0.5 * contrast + 0.5) * 255).toFloat()
        return floatArrayOf(
            contrast, 0f,      0f,      0f, adjust,
            0f,      contrast, 0f,      0f, adjust,
            0f,      0f,      contrast, 0f, adjust,
            0f,      0f,      0f,      1f, 0f
        )
    }

    private fun createSepiaMatrix(intensity: Float): FloatArray {
        return floatArrayOf(
            0.393f * intensity + (1 - intensity), 0.769f * intensity,         0.189f * intensity,         0f, 0f,
            0.349f * intensity,                   0.686f * intensity + (1 - intensity), 0.168f * intensity,         0f, 0f,
            0.272f * intensity,                   0.534f * intensity,         0.131f * intensity + (1 - intensity), 0f, 0f,
            0f,                                   0f,                                   0f,                                   1f, 0f
        )
    }

    private fun createSaturationMatrix(saturation: Float): FloatArray {
        return floatArrayOf(
            0.213f + 0.787f * saturation, 0.715f - 0.715f * saturation, 0.072f - 0.072f * saturation, 0f, 0f,
            0.213f - 0.213f * saturation, 0.715f + 0.285f * saturation, 0.072f - 0.072f * saturation, 0f, 0f,
            0.213f - 0.213f * saturation, 0.715f - 0.715f * saturation, 0.072f + 0.928f * saturation, 0f, 0f,
            0f,                          0f,                          0f,                          1f, 0f
        )
    }

    private fun createInvertMatrix(amount: Float): FloatArray {
        return floatArrayOf(
            1f - 2f * amount, 0f,               0f,               0f, 255f * amount,
            0f,              1f - 2f * amount,  0f,               0f, 255f * amount,
            0f,              0f,              1f - 2f * amount,  0f, 255f * amount,
            0f,              0f,              0f,              1f, 0f
        )
    }

    private fun createHueMatrix(degrees: Float): FloatArray {
        val radians = degrees * PI.toFloat() / 180f
        val cos = cos(radians)
        val sin = sin(radians)

        return floatArrayOf(
            0.213f + cos * 0.787f - sin * 0.213f, 0.715f - cos * 0.715f - sin * 0.715f, 0.072f - cos * 0.072f + sin * 0.928f, 0f, 0f,
            0.213f - cos * 0.213f + sin * 0.143f, 0.715f + cos * 0.285f + sin * 0.140f, 0.072f - cos * 0.072f - sin * 0.283f, 0f, 0f,
            0.213f - cos * 0.213f - sin * 0.787f, 0.715f - cos * 0.715f + sin * 0.715f, 0.072f + cos * 0.928f + sin * 0.072f, 0f, 0f,
            0f,                                 0f,                                 0f,                                 1f, 0f
        )
    }
}
